*********************************************************************************
*																				*
*	Campaign Panel Book Chapter 2 - Auxilliary and control variables			*
*																				*
*********************************************************************************

*Here: coding of all auxilliary- and control variables for chapter 2
cd "${path}\Reproducible Do-Files\Chapter 2"

*---------------------------------------------------------------------
* Voting intention including all parties + postal voters + non voters
*---------------------------------------------------------------------

labelbook kp1_190bb
lab define kp1_190bb ///
	888 "weiß nicht" ///
	999 "werde nicht wählen", modify

* Unify coding of vote intention waves 1 to 6	
foreach welle of numlist 1/6 {
	gen a_wabsfull`welle' = a_kp`welle'_190bc
	recode a_wabsfull`welle' (9=206) (10=225) (13 11=149) ///
		(12 14 15 16 18 19 21 31 35 40 43 50 83=801) (44=180) ///
		(46=209) (47=215) (83=801) (98=888) (93 99 100=.) 
	replace a_wabsfull`welle' = 999 if a_kp`welle'_170==4 ///
		| a_kp`welle'_170==5 | a_kp`welle'_170==98 
	gen b_wabsfull`welle' = b_kp`welle'_190bb
	recode b_wabsfull`welle' (.a=.) (.b=888) (.c .e .g=.)
	replace b_wabsfull`welle' = 999 if b_kp`welle'_170==4 ///
		| b_kp`welle'_170==5 
	lab var a_wabsfull`welle' "Wahlabsicht mit allen Parteien + Briefwähler + Nichtwähler"
	lab var b_wabsfull`welle' "Wahlabsicht mit allen Parteien + Briefwähler + Nichtwähler"
}

* Actual vote wave 7
gen a_wabsfull7 = a_kp7_200bc
recode a_wabsfull7 (9=206) (10=225) (13 11=149) ///
		(12 14 15 16 17 18 19 21 31 35 40 43 50 83=801) (44=180) ///
		(46=209) (47=215) (83=801) (93 98 99 100=.) 
replace a_wabsfull7 = 999 if a_kp7_180==2

gen b_wabsfull7 = b_kp7_200bb
recode b_wabsfull7 (.a .b .c .e .g=.) (.k=999)
replace b_wabsfull7 = 999 if b_kp7_180==2

lab val *wabsfull* kp1_190bb


fre a_wabsfull?
fre b_wabsfull?


* Integration of postal voters

// 2009 problem: no dependent interviews, what counts is the first interview as postal voter
foreach welle of numlist 3/7 {
replace a_wabsfull`welle'= a_kp3_191bc if a_kp3_170==6
}

foreach welle of numlist 4/7 {
replace a_wabsfull`welle' = a_kp4_191bc if a_kp4_170==6 & a_kp3_170~=6
}

foreach welle of numlist 5/7 {
replace a_wabsfull`welle' = a_kp5_191bc if a_kp5_170==6 & a_kp4_170~=6 & a_kp3_170~=6
}

foreach welle of numlist 6/7 {
replace a_wabsfull`welle' = a_kp6_191bc if a_kp6_170==6 & a_kp5_170~=6 & a_kp4_170~=6 & a_kp3_170~=6
}

foreach welle of numlist 3/7 {
recode a_wabsfull`welle' (9=206) (10=225) (13 11=149) ///
		(12 14 15 16 18 19 21 31 35 40 43 50 83=801) (44=180) ///
		(46=209) (47=215) (83=801) (93 98 99 100=.)  //Postal voters are not allowed to answer "I don't know"
}


fre a_wabsfull?

//2013
foreach welle of numlist 3/6 {			
replace b_wabsfull`welle' = b_kp`welle'_191bb if b_kp`welle'_170==6
recode b_wabsfull`welle' (.a .b .c .e .g=.)
}


replace b_wabsfull7 = b_kp7_191bb if b_kp7_180==6
recode b_wabsfull7 (.a .b .c .e .g=.)

foreach welle of numlist 4/7 {
replace b_wabsfull`welle' = b_wabsfull3 if b_kp3_170==6
}

foreach welle of numlist 5/7 {
replace b_wabsfull`welle' = b_wabsfull4 if b_kp4_170==6
}

foreach welle of numlist 6/7 {
replace b_wabsfull`welle' = b_wabsfull5 if b_kp5_170==6
}

foreach welle of numlist 7/7 {
replace b_wabsfull`welle' = b_wabsfull6 if b_kp6_170==6
}

fre b_wabsfull?

* Partyvotedummies
foreach year in a b {
foreach num of numlist 1/7 {
tab `year'_wabsfull`num', gen(`year'_voti`num')	//creates dummies automatically
rename `year'_voti`num'1 `year'_cduvote`num'	//rename dummies with sensible names
rename `year'_voti`num'2 `year'_spdvote`num'
rename `year'_voti`num'3 `year'_fdpvote`num'
rename `year'_voti`num'4 `year'_gruvote`num'
rename `year'_voti`num'5 `year'_linkvote`num'
lab var `year'_cduvote`num' "CDU/CSU"
lab var `year'_spdvote`num' "SPD"
lab var `year'_fdpvote`num' "FDP" 
lab var `year'_gruvote`num' "Bündnis '90/Die Grünen"
lab var `year'_linkvote`num' "Die Linke"
}
}

drop *voti*



tab a_wabsfull1 a_cduvote1, m


*--------------------------------------------------------------------------------------
* Voting intention without breakdown of other parties, but including postal voters and non-voters
*--------------------------------------------------------------------------------------

lab define wabs ///
		   1   "CDU/CSU" ///
           4   "SPD" ///
           5   "FDP" ///
           6   "GRUENE" ///
           7   "DIE LINKE" ///
         801   "andere Partei" ///
         888   "weiß nicht" ///
         999   "werde nicht wählen"


* Unify coding of vote intention waves 1 to 6		 
foreach welle of numlist 1/6 {
	gen a_wabsbrief`welle' = a_kp`welle'_190b
	recode a_wabsbrief`welle' (8=801) (98=888) (93 99 100=.) 
	replace a_wabsbrief`welle' = 999 if a_kp`welle'_170==4 ///
		| a_kp`welle'_170==5 | a_kp`welle'_170==98 
	replace a_wabsbrief`welle' = . if a_kp`welle'_190bc==93	//Exclusion of "other mention/no party" = B answers something not identifiable as party
	gen b_wabsbrief`welle' = b_kp`welle'_190ba
	recode b_wabsbrief`welle' (.a=.) (.b=888) (.c .e .g=.)
	replace b_wabsbrief`welle' = 999 if b_kp`welle'_170==4 ///
		| b_kp`welle'_170==5 
	lab var a_wabsbrief`welle' "Wahlabsicht mit Briefwähler + Nichtwähler"
	lab var b_wabsbrief`welle' "Wahlabsicht mit Briefwähler + Nichtwähler"
}

foreach welle of numlist 1/6 {
tab a_wabsbrief`welle' a_kp`welle'_190b, m
}

* Actual vote wave 7
gen a_wabsbrief7 = a_kp7_200b
recode a_wabsbrief7 (8=801) (98 99 100=.) 
replace a_wabsbrief7 = 999 if a_kp7_180==2
replace a_wabsbrief7 = . if a_kp7_200bc==93 //Ausschluss von "andere Nennung/keine Partei" = = B antwortet irgendwas, was nicht als Partei zu identifizieren ist

gen b_wabsbrief7 = b_kp7_200ba
recode b_wabsbrief7 (.a .b .c .e .g=.) (.k=999)
replace b_wabsbrief7 = 999 if b_kp7_180==2

lab val *wabsbrief* wabs


fre a_wabsbrief?
fre b_wabsbrief?


* Integration of postal voters

//2009 problem: no dependent interviews, what counts is the first interview as postal voter
foreach welle of numlist 3/7 {
replace a_wabsbrief`welle'= a_kp3_191b if a_kp3_170==6
}

foreach welle of numlist 4/7 {
replace a_wabsbrief`welle' = a_kp4_191b if a_kp4_170==6 & a_kp3_170~=6 & a_kp4_191bc~=93
}

foreach welle of numlist 5/7 {
replace a_wabsbrief`welle' = a_kp5_191b if a_kp5_170==6 & a_kp4_170~=6 & a_kp3_170~=6 & a_kp5_191bc~=93
}

foreach welle of numlist 6/7 {
replace a_wabsbrief`welle' = a_kp6_191b if a_kp6_170==6 & a_kp5_170~=6 & a_kp4_170~=6 & a_kp3_170~=6 & a_kp6_191bc~=93
}

foreach welle of numlist 3/7 {
recode a_wabsbrief`welle' (8=801) (98 99=.)   //Postal voters are not allowed to answer "I don't know"
}


fre a_wabsbrief?

//2013
foreach welle of numlist 3/6 {			
replace b_wabsbrief`welle' = b_kp`welle'_191ba if b_kp`welle'_170==6
recode b_wabsbrief`welle' (.a .b .c .e .g=.)
}


replace b_wabsbrief7 = b_kp7_191ba if b_kp7_180==6
recode b_wabsbrief7 (.a .b .c .e .g=.)

foreach welle of numlist 4/7 {
replace b_wabsbrief`welle' = b_wabsbrief3 if b_kp3_170==6
}

foreach welle of numlist 5/7 {
replace b_wabsbrief`welle' = b_wabsbrief4 if b_kp4_170==6
}

foreach welle of numlist 6/7 {
replace b_wabsbrief`welle' = b_wabsbrief5 if b_kp5_170==6
}

foreach welle of numlist 7/7 {
replace b_wabsbrief`welle' = b_wabsbrief6 if b_kp6_170==6
}

fre b_wabsbrief?

* Control
foreach num of numlist 1/7 {
tab a_wabsfull`num' a_wabsbrief`num', m
tab b_wabsfull`num' b_wabsbrief`num', m
}
fsum a_wabs*

tab a_kp1_190bc if a_wabsfull1==. & a_wabsbrief1~=.

*-------------------------------------------------
* Voting intention conversation partner
*--------------------------------------------------

fre a_kp6_1970
lab define wabsgp ///
		   1   "CDU/CSU" ///
           4   "SPD" ///
           5   "FDP" ///
           6   "GRUENE" ///
           7   "DIE LINKE" ///
         801   "andere Partei" ///
         888   "weiß nicht/GP hat sich noch nicht entschieden" ///
         999   "GP wird nicht wählen",replace

foreach num of numlist 2/7 {
recode a_kp`num'_1970 (8=801) (991 998=888) (993 995=999) (996 999 1000=.), ///
	gen(a_wabsfull_gp`num')
recode b_kp`num'_1970 (.a .c .e .g=.) (.b=888) (.j=999) (.=888), ///
	gen(b_wabsfull_gp`num')
}

lab val *wabsfull_gp* wabsgp

*Control
tab a_wabsfull_gp2 a_kp2_1970, m 
tab b_wabsfull_gp2 b_kp2_1970, m 
fre a_kp2_1970


*-----------------------------------------------------------------------
* Turnout intention: Dummy + postal voters
*-----------------------------------------------------------------------

* Dummies
foreach welle of numlist 1/6 {
foreach year in a b {
	gen `year'_wbtdummy`welle' = 1 if `year'_kp`welle'_170<=3 | `year'_kp`welle'_170==6
	replace `year'_wbtdummy`welle' = 0 if (`year'_kp`welle'_170>3 ///
		& `year'_kp`welle'_170<=5) | `year'_kp`welle'_170==98
	lab var `year'_wbtdummy`welle' "Wahlbeteiligung Welle `welle'"
}
}

foreach year in a b {
recode `year'_kp7_180 (2=0) (6=1) (.a .c .e 99=.), gen(`year'_wbtdummy7)
lab var `year'_wbtdummy7 "Wahlbeteiligung Welle 7"
} 

tab a_kp1_170 a_wbtdummy1, m


* Integration of postal voters

//2009 problem: no dependent interviews, what counts is the first interview as postal voter
foreach welle of numlist 3/7 {
replace a_wbtdummy`welle'= 1 if a_kp3_170==6
}

foreach welle of numlist 4/7 {
replace a_wbtdummy`welle' = 1 if a_kp4_170==6 & a_kp3_170~=6
}

foreach welle of numlist 5/7 {
replace a_wbtdummy`welle' = 1 if a_kp5_170==6 & a_kp4_170~=6 & a_kp3_170~=6
}

foreach welle of numlist 6/7 {
replace a_wbtdummy`welle' = 1 if a_kp6_170==6 & a_kp5_170~=6 & a_kp4_170~=6 & a_kp3_170~=6
}


fre a_wbtdummy?

//2013
foreach welle of numlist 4/7 {
replace b_wbtdummy`welle' = 1 if b_kp3_170==6
}

foreach welle of numlist 5/7 {
replace b_wbtdummy`welle' = 1 if b_kp4_170==6
}

foreach welle of numlist 6/7 {
replace b_wbtdummy`welle' = 1 if b_kp5_170==6
}

foreach welle of numlist 7/7 {
replace b_wbtdummy`welle' = 1 if b_kp6_170==6
}

fre b_wbtdummy?


*-------------------------------------------------------------------------
* Strength of party identification
*--------------------------------------------------------------------------


foreach welle of numlist 1 3 5 7 {
recode a_kp`welle'_2100 (1=1) (2=0.75) (3=0.5) (4=0.25) (5=0) ///
	(else=.), gen(a_pid_staerke`welle')
replace a_pid_staerke`welle' = 0 if a_kp`welle'_2090==808
lab var a_pid_staerke`welle' "Stärke Parteiidentifikation"
}

tab a_pid_staerke1 a_kp1_2100, m

* 2009 fill up W2-starter variable from wave 1 with values from wave 3
replace a_pid_staerke1 = a_pid_staerke3 if a_w1==0

fre a_pid_staerke1 if a_w1==0

*-------------------------------------------------------
* Partyidentificationvariable direction
*-------------------------------------------------------


foreach num in 1 3 5 7 {
recode a_kp`num'_2090 (2/3=1 "CDU/CSU") (4=4 "SPD") ///
	(5=5 "FDP") (6=6 "Greens") (7=7 "The Left") (801=801 "andere Partei") ///
	(808=808 "no party") (997 999=.), gen(a_pidfull`num')
recode b_kp`num'_2090a (2/3=1) (.a=.), gen(b_pidfull`num')
lab val b_pidfull`num' kp1_2090b 
}

tab b_kp1_2090b b_pidfull1, m
tab a_kp1_2090ac a_pidfull1, m

* 2009 fill up W2-starter variable from wave 1 with values from wave 3
replace a_pidfull1 = a_pidfull3 if a_w1==0

fre a_pidfull1 
fre a_w1



*------------------------------------------------------------------------
* Partyidentification direction dummies
*------------------------------------------------------------------------

foreach year in a b {
foreach welle of numlist 1 3 5 7 {
gen `year'_pidcdu`welle' = `year'_kp`welle'_2090a<=3
gen `year'_pidspd`welle' = `year'_kp`welle'_2090a==4
gen `year'_pidfdp`welle' = `year'_kp`welle'_2090a==5
gen `year'_pidgru`welle' = `year'_kp`welle'_2090a==6
gen `year'_pidlink`welle' = `year'_kp`welle'_2090a==7
gen `year'_pidno`welle' = `year'_kp`welle'_2090a==808
lab var `year'_pidcdu`welle' "PID CDU/CSU"
lab var `year'_pidspd`welle' "PID SPD" 
lab var `year'_pidfdp`welle' "PID FDP" 
lab var `year'_pidgru`welle' "PID Grüne" 
lab var `year'_pidlink`welle' "PID Linke" 
lab var `year'_pidno`welle' "Keine PID"
}
}

foreach year in a b {
foreach partei in cdu spd fdp gru link no {
foreach welle of numlist 1 3 5 7 {
replace `year'_pid`partei'`welle' = . if `year'_kp`welle'_2090a>808 ///
	| `year'_kp`welle'_2090a==999 | `year'_kp`welle'_2090a==.
}
}
}

* 2009 fill up W2-starter variable from wave 1 with values from wave 3
foreach var in pidcdu pidspd pidfdp pidgru pidlink pidno {
replace a_`var'1 = a_`var'3 if a_w1==0
}

tab a_pidcdu1 a_pidcdu3 if a_w1==0, m
tab a_pidno1 a_kp3_2090a if a_w1==0
tab a_kp3_2090a a_pidcdu3 if a_w1==0, m


*-------------------------------------------------------------
* Needs
*---------------------------------------------------------------

* Need for cognitive closure
fre a_kp4_1570b //Satisfaction thinking
fre b_kp1_1570a //Satisfaction thinking


gen a_needclos = a_kp4_1570b if a_kp4_1570b~=99
gen b_needclos = b_kp1_1570a if b_kp1_1570a~=99
recode ?_needclos (5=4)

lab var a_needclos "Need for cognitive closure"
lab var b_needclos "Need for cognitive closure"

* Need to evaluate
fre a_kp4_1570a //an opinion on everything
fre a_kp4_1570c //decisions fast and certain

fre b_kp1_1570f //secure point of view
fre b_kp1_1570e //an opinion on everything

recode a_kp4_1570a (2=1) (3=2) (4=3) (5=4) (99=.), gen(a_help1)
recode a_kp4_1570c (2=1) (3=2) (4=3) (5=4) (99=.), gen(a_help2)

recode b_kp1_1570f (2=1) (3=2) (4=3) (5=4), gen(b_help1)
recode b_kp1_1570e (2=1) (3=2) (4=3) (5=4), gen(b_help2)

fre *help*

egen a_needeval = rowmean(a_help1 a_help2)
egen b_needeval = rowmean(b_help1 b_help2)

lab var a_needeval "Need for evaluation"
lab var b_needeval "Need for evaluation"

drop *help*

*---------------------------------------------------------------------------
* Type formation: stability and instability in partyvoteintention
*----------------------------------------------------------------------------

 lab def partisan ///
	1 "stabile Wabs" ///
	0 "instabile Wabs"

foreach year in a b {
egen `year'_partisan = diff(`year'_wabsfull?)
recode `year'_partisan (0=1) (1=0)
replace `year'_partisan = . if `year'_wabsfull1 + ///
	`year'_wabsfull2 + `year'_wabsfull3 + `year'_wabsfull4 + ///
	`year'_wabsfull5 + `year'_wabsfull6 + `year'_wabsfull7 == 6993 //7*999 (Code for "will not turn out" = 6993)
replace `year'_partisan = . if `year'_wabsfull7==.
lab var `year'_partisan "Stabilität Parteiwahlabsicht"
}

fre a_partisan

list a_wabsfull? a_partisan if a_w1==0

* 2009 W2-starter
replace a_partisan = 1 if a_w1==0 & a_wabsfull2==a_wabsfull7 & ///
	a_wabsfull3==a_wabsfull7 & a_wabsfull4==a_wabsfull7 & ///
	a_wabsfull5==a_wabsfull7 & a_wabsfull6==a_wabsfull7 & ///
	a_wabsfull7~=999 & a_wabsfull7~=.
	
list a_wabsfull? a_partisan if a_w1==0, nol	

* Exclude missings (did not participate in all waves)	
replace a_partisan = . if a_w1==0 & a_teilnahme<6		//only for respondents who participated in all waves
replace a_partisan = . if a_w1==1 & a_teilnahme<7
replace b_partisan = . if b_n_teilnahme<7	//only for respondents who participated in all waves


* label and check	
lab val *partisan partisan	
fre a_partisan
list a_wabsfull? a_partisan, nol 
list b_wabsfull? b_partisan, nol

fre *partisan

list b_wabsfull? if b_partisan==1
list a_wabsfull? if a_partisan==1



*-------------------------------------------------------------------------
* Type formation: Stability and instability in turnout intention
*--------------------------------------------------------------------------

foreach year in a b {
gen `year'_help1 = `year'_wabsfull1==999 & `year'_wabsfull2==999 ///
	& `year'_wabsfull3==999 & `year'_wabsfull4==999 & `year'_wabsfull5==999 ///
	& `year'_wabsfull6==999 & `year'_wabsfull7==999
gen `year'_help2 = `year'_wabsfull1<999 & `year'_wabsfull2<999 ///
	& `year'_wabsfull3<999 & `year'_wabsfull4<999 & `year'_wabsfull5<999 ///
	& `year'_wabsfull6<999 & `year'_wabsfull7<999
gen `year'_wbtstabil = `year'_help1==1 | `year'_help2==1
replace `year'_wbtstabil = . if missing(`year'_wabsfull1, `year'_wabsfull2, ///
	`year'_wabsfull3, `year'_wabsfull4, `year'_wabsfull5, `year'_wabsfull6, ///
	`year'_wabsfull7)
lab var `year'_wbtstabil "Stabilität Wahlbeteiligungsabsicht"
}

list b_wabsfull? b_wbtstabil if b_wabsfull1==999, nol
list a_wabsfull? a_wbtstabil if a_w1==0
fre b_wbtstabil

* 2009 W2-Starter need to be added
replace a_wbtstabil = 1 if a_wbtstabil==. & a_w1==0 & ///
	a_wabsfull2==999 & a_wabsfull3==999 & a_wabsfull4==999 & ///
	a_wabsfull5==999 & a_wabsfull6==999 & a_wabsfull7==999 
replace a_wbtstabil  = 1 if a_wbtstabil==. & a_w1==0 & ///
	a_wabsfull2<999 & a_wabsfull3<999 & a_wabsfull4<999 & a_wabsfull5<999 ///
	& a_wabsfull6<999 & a_wabsfull7<999 
replace a_wbtstabil = 0 if a_wbtstabil==. & a_w1==0 & ///
	!missing(a_wabsfull2, a_wabsfull3, a_wabsfull4, ///
	a_wabsfull5, a_wabsfull6, a_wabsfull7)

tab a_wbtstabil a_teilnahme, m
list a_wabsfull? a_wbtstabil if a_teilnahme<6 & a_wbtstabil~=.	//assumption: these are postal voters
	
	
* Valuelabel
lab define yesno 0 "nein" 1 "ja"
lab val *wbtstabil yesno
	
*Control
fre a_wbtstabil if a_w1==0
list a_wabsfull? a_wbtstabil if a_w1==0

list b_wabsfull? b_wbtstabil if b_wabsfull1==999, nol


*Frequency distribution
fre *wbtstabil			//close to 85% are stable in their decision

drop *help*


